<script setup lang="ts">
import { useSettingsStore } from '@/stores/modules/settings'
import { useUserInfoStore } from '@/stores/modules/userInfo'
import { Lock, User } from '@element-plus/icons-vue'
import { type FormInstance, type FormRules } from 'element-plus'
import { useRouter } from 'vue-router'

const { systemTitle } = toRefs(useSettingsStore())

interface FormData {
  username: string
  password: string
}
const formData = ref<FormData>({
  username: 'admin',
  password: '123456'
})
const formRef = ref<FormInstance>()
const formRules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
})

const handleReset = () => {
  formRef.value?.resetFields()
}

const router = useRouter()
const { userLogin } = useUserInfoStore()
const handleLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async valid => {
    if (valid) {
      await userLogin(formData.value)
      router.push('/')
    }
  })
}
</script>

<template>
  <div class="login-box">
    <el-row :gutter="10">
      <el-col :span="16" class="col-left">
        <zc-svg-icon name="bg-cover" size="850px" />
      </el-col>
      <el-col :span="8" class="col-right">
        <el-card class="login-form" shadow="hover">
          <div class="system-title">
            <zc-svg-icon name="logo" size="50px" />
            <span>{{ systemTitle }}</span>
          </div>
          <el-form ref="formRef" :rules="formRules" :model="formData">
            <el-form-item prop="username">
              <el-input
                :prefix-icon="User"
                v-model="formData.username"
                placeholder="请输入用户名"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                :prefix-icon="Lock"
                v-model="formData.password"
                placeholder="请输入密码"
                type="password"
                show-password
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item class="login-btn">
              <el-button type="primary" class="submit-btn" @click="handleLogin(formRef)"
                >登录</el-button
              >
              <el-button class="reset-btn" @click="handleReset">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login-box {
  width: 100vw;
  height: 100vh;
  min-width: 1280px;
  min-height: 800px;
  background: linear-gradient(135deg, #f5f7fa 0%, #dde2e9 100%);

  .el-row {
    height: 100%;

    .col-left {
      box-shadow: var(--el-box-shadow-lighter);
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: #fff;
      border-radius: 0 8px 8px 0;
    }

    .col-right {
      display: flex;
      justify-content: center;
      align-items: center;

      .login-form {
        width: 450px;
        border-radius: 8px;
        padding: 20px;
        transition: all 0.3s ease;

        &:hover {
          transform: translateY(-5px);
        }

        .system-title {
          width: 100%;
          font-size: 32px;
          font-weight: 600;
          margin: 20px 0 40px;
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 15px;
          letter-spacing: 1px;
        }

        .el-input {
          height: 42px;
          margin-bottom: 5px;
        }
      }
    }
  }
}

.login-btn {
  width: 100%;
  margin-top: 40px;
  :deep(.el-form-item__content) {
    display: flex;
    justify-content: flex-end;
    gap: 15px;

    .submit-btn,
    .reset-btn {
      width: 120px;
      height: 42px;
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      }
    }

    .submit-btn {
      font-weight: 600;
      letter-spacing: 2px;
    }
  }
}
</style>
